package com.xiangxiangjd.kj.papersstar.controller.api;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiangxiangjd.kj.papersstar.aop.UserLoginToken;
import com.xiangxiangjd.kj.papersstar.bean.User;
import com.xiangxiangjd.kj.papersstar.controller.BaseController;
import com.xiangxiangjd.kj.papersstar.controller.entity.ApiCode;
import com.xiangxiangjd.kj.papersstar.controller.helper.$;
import com.xiangxiangjd.kj.papersstar.controller.model.ApiResultModel;
import com.xiangxiangjd.kj.papersstar.mapper.UserMapper;
import com.xiangxiangjd.kj.papersstar.service.TokenService;
import com.xiangxiangjd.kj.papersstar.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 肖守凯
 * @date 2018-07-08 20:45
 */
@Api(tags = "个人")
@RestController
@RequestMapping("/api/user/")
public class UserApi extends BaseController {

    @Autowired
    TokenService tokenService;


    @Autowired
    private UserMapper userMapper;

    //登录
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Object login(@RequestBody User user) {
        System.out.println("进入登录");
        //第一种登录方式   用户名+密码

        if (user != null && user.getPassword() != null) {
            //如果密码不为空则加密验证
            user.setPassword(MD5Util.string2MD5(user.getPassword()));
        } else {
            return $.error(500, "密码不能为空");
        }
        //JSONObject jsonObject = new JSONObject();
        User userForBase = userService.findByUsername(user.getUsername());
        if (userForBase == null) {
            /*jsonObject.put("message","登录失败,用户不存在");
            return jsonObject;*/
            return $.error(500, "登录失败,用户不存在");
        } else {
            if (!userForBase.getPassword().equals(user.getPassword())) {
                /*jsonObject.put("message","登录失败,密码错误");
                return jsonObject;*/
                return $.error(500, "登录失败,密码错误");
            } else {
                String token = tokenService.getToken(userForBase);


                //登陆成功更新用户token
                userForBase.setToken(token);
                userForBase.setUpdateTime(String.valueOf(new Date().getTime()));//登录时间
                userForBase.setFirstLogin(userForBase.getFirstLogin() + 1);
                userService.UpdateUserToken(userForBase);
                return $.success(userForBase);
            }
        }


    }

    @ApiOperation(value = "注册用户")
    @PutMapping("/register")
    public Object reGister(@RequestBody User user) {


        User byUsername = userService.findByUsername(user.getUsername());


        if (byUsername != null) {
            return $.error("该用户名已存在!");
        }


        //默认用户状态为1
        user.setStatus(User.Status.VALID);
        user.setFirstLogin(0);

        user.setPassword(MD5Util.string2MD5(user.getPassword()));
        //保存用户
        boolean save = userService.save(user);

        if (save) return $.success();

        return $.error("注册失败");
    }

    //查询所有用户
    @GetMapping("list")
    @UserLoginToken
    public ApiResultModel getAllUser() {
        List<User> list = userService.list();


        return $.success(ApiCode.success, ApiCode.success_msg, list);
    }


    //退出登录
    @DeleteMapping
    public ApiResultModel layout(String id) {
        User user = new User();
        user.setToken("");


        User userLayout = new User();
        userLayout.setId(id);

        boolean update = userService.update(user, new UpdateWrapper(userLayout));


        return update ? $.success() : $.error();
    }

}